//
//  PlayMoveViewController.m
//  BigToBig
//
//  Created by admin on 16/5/9.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "PlayMoveViewController.h"
#import <KaratLivePlayerSDK/KaratLivePlayerSDK.h>


@interface PlayMoveViewController()
@property (weak ,nonatomic)UILabel *pepoleCountLb;
@property (strong ,nonatomic)NSTimer *timer;

@end
@implementation PlayMoveViewController


- (void)viewDidLoad{

    [super viewDidLoad];
//
   NSInteger str = [self.playStr intValue];
//    NSLog(@"%d",str);
//
    NSString *PlayStr = [NSString stringWithFormat:@"rtmp://sdk1.karat.tv:1935/live/%d",str];
    NSInteger origentionScreen = [self.origentionScreen intValue];
     [self initPlayerView:PlayStr withScreenType:origentionScreen];
    
    
    self.delegate = self;

    [self startPlayer];
    
    [self makeUI];
    
    
}


- (NSString *)md5HexDigest:(NSString*)password
{
    
    const char *original_str = [password UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(original_str, strlen(original_str), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
    {
        [hash appendFormat:@"%02X", result[i]];
    }
    NSString *mdfiveString = [hash lowercaseString];
    
    NSLog(@"Encryption Result = %@",mdfiveString);
    return mdfiveString;
}

- (void)makeUI{
  
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 126, 36)];
    imageView.image = [UIImage imageNamed:@"zhibobg"];
    [self.view addSubview:imageView];
    
    UIImageView *headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 36, 36)];
    headImage.layer.masksToBounds = YES;
    headImage.layer.cornerRadius = 18;
    [imageView addSubview:headImage];
    
    
    UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(headImage.maxX + 5, 0, 60, 18)];
    nameLb.text = self.nameStr;
    nameLb.textColor = [UIColor whiteColor];
    [imageView addSubview:nameLb];
    
    UILabel *pepoleCountLb = [[UILabel alloc]initWithFrame:CGRectMake(headImage.maxX + 5, 18, 60, 18)];
    pepoleCountLb.font = [UIFont systemFontOfSize:10.0];
    pepoleCountLb.textColor = [UIColor whiteColor];
    pepoleCountLb.text = @"0人在线";
    self.pepoleCountLb = pepoleCountLb;
    [imageView addSubview:pepoleCountLb];
    
    NSString *userIdStr = [NSString stringWithFormat:@"%@",self.userID];
    NSString *str = [self md5HexDigest:userIdStr];
    NSString *b = [str substringWithRange:NSMakeRange(8,16)];
    NSString *str1 = @".png";
    NSString *imgName = [NSString stringWithFormat:@"%@%@",b,str1];
    NSLog(@"%@",imgName);
    
    
    NSString *HeadImageUrl = [NSString stringWithFormat:@"http://61.135.176.43:6080/data/HeadPortrait/%@",imgName];
    
    NSLog(@"%@",HeadImageUrl);
    
    
    UIImageView *imageView1 = [[UIImageView alloc]init];
    
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:HeadImageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        //设置头像图片
        if (error) {
            
            [headImage setImage:[UIImage imageNamed:@"tx"]];
            
            return ;
        }
        headImage.image = imageView1.image;
        
        
    }];

}
- (void)startTimer{
    
    NSTimer *timer = [[NSTimer alloc]init];
    timer =  [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(loadPepoleCount) userInfo:nil repeats:YES];
    self.timer = timer;
    [timer fire];
    
    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop]run];
}

- (void)viewWillAppear:(BOOL)animated{

    NSThread *t =  [[NSThread alloc]initWithTarget:self selector:@selector(startTimer) object:nil];
    [t start];
}

- (void)viewWillDisappear:(BOOL)animated{

    [self.timer invalidate];
}
- (void)onBtnCloseDelegate
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadPepoleCount{
    
    
    NSThread *thred = [NSThread currentThread];
    NSLog(@"%@",thred);
    NSDictionary *params = @{@"activityId":self.playStr};
    [HttpTool postWithURL:@"http://sdk2.karat.tv:8081/activity/onlineVeiwers?" params:params success:^(id json) {
        
        NSString *str = json[@"payload"];
        NSLog(@"%@",str);
        NSString *pepoleCountStr = [NSString stringWithFormat:@" %@人在线",str];
        self.pepoleCountLb.text = pepoleCountStr;
        
    } failure:^(NSError *error) {
        
        
    }];
    
}
@end
